package com.os.ordersystem.service.impl;

import com.os.ordersystem.bean.User;
import com.os.ordersystem.mapper.UserMapper;
import com.os.ordersystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int register(User user) {

        return userMapper.register(user);
    }

    @Override
    public User login(String name, String password) {

        return userMapper.login(name, password);
    }

    @Override
    public int updateAvatar(String pic, Integer id) {
        return userMapper.updateAvatar(pic, id);
    }

    @Override
    public long getTotal(String name) {
        return userMapper.getTotal(name);
    }

    @Override
    public List<User> queryUserList(Map<String, Object> map) {
        return userMapper.queryUserList(map);
    }

    @Override
    public int updateUserInfo(User user) {

        return userMapper.updateUserInfo(user);
    }

    @Override
    public int deleteUserInfo(Integer id) {

        return userMapper.deleteUserInfo(id);
    }

    @Override
    public int insertIntegral(Integer userId, Double sum) {
        User user = userMapper.selectUserInfo(userId);
        if (user!=null){
            Integer oldIntegral = user.getIntegral();
            int newIntegral = (int) (oldIntegral+ sum);
            user.setIntegral(newIntegral);
        }
        return userMapper.updateUserInfo(user);
    }
}
